package com.shareinfo.adminManage.auto.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.shareinfo.adminManage.auto.model.AutoDbModel;
import com.shareinfo.adminManage.auto.query.AutoDbQuery;
import com.shareinfo.adminManage.auto.service.AutoDbService;
import com.shareinfo.base.pagination.PageInfo;

@Controller
@Scope("prototype")
@RequestMapping(value = "/adminManage/auto/autoDb")
public class AutoDbController {
	
  @Resource(name = "autoDbService")
  private AutoDbService autoDbService;
  
  @ModelAttribute
	public AutoDbModel get(@RequestParam(required=false) java.lang.String id) {
		if (id!=null){
			return autoDbService.getById(id);
		}else{
			return new AutoDbModel();
		}
	}
  
  	/** 
	 * 列出对象
	**/
	@RequestMapping(value = {"listForAjax"})
	@ResponseBody
	public Map<String, Object> listForAjax(AutoDbQuery autoDbQuery) {
		List<AutoDbModel> list = autoDbService.selectAll(autoDbQuery);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(PageInfo.EASYUI_ROWS, list);
		map.put(PageInfo.EASYUI_TOTAL, list.size());
		return map;
	}
	
	/** 
	 * 查看对象
	 **/
	@RequestMapping(value = {"showForAjax"})
	@ResponseBody
	public Map<String, Object> showForAjax(AutoDbQuery autoDbQuery) throws Exception {
		AutoDbModel autoDb = (AutoDbModel)autoDbService.getById(autoDbQuery.getId());
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("autoDb", autoDb);
		return map;
	}
	
	@RequestMapping(value = {"add"})
	public ModelAndView add(HttpServletRequest request) {
		return new ModelAndView("autoDb");
	}
	
	/** 
	 * 保存新增对象
	 **/
	@RequestMapping(value = {"saveForAjax"})
	@ResponseBody
	public Map<String, Object> saveForAjax(AutoDbQuery autoDbQuery) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		AutoDbModel autoDbModel = new AutoDbModel();
		new BeanUtilsBean().copyProperties(autoDbModel, autoDbQuery);
		int ret = autoDbService.save(autoDbModel);
		if(ret==0)
			map.put("result", false);
		else
			map.put("result", true);
		return map;
	}
	
	@RequestMapping(value = {"update"})
	public ModelAndView update(AutoDbQuery autoDbQuery) {
		AutoDbModel autoDb = (AutoDbModel)autoDbService.getById(autoDbQuery.getId());
		ModelAndView view = new ModelAndView("autoDb");
		view.addObject("autoDb", autoDb);
		return view;
	}
	
	/**
	 * 保存更新对象
	 **/
	@RequestMapping(value = {"updateForAjax"})
	@ResponseBody
	public Map<String, Object> updateForAjax(AutoDbQuery autoDbQuery) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		AutoDbModel autoDbModel = null;
		if(autoDbQuery.getId()!=null){
			autoDbModel = (AutoDbModel)autoDbService.getById(autoDbQuery.getId());
		}else{
			autoDbModel = new AutoDbModel();
		}
		new BeanUtilsBean().copyProperties(autoDbModel, autoDbQuery);
		int ret=autoDbService.update(autoDbModel);
		if(ret==0)
			map.put("result", false);
		else
			map.put("result", true);
		return map;
	}
	
	/**
	 *删除对象
	 **/
	@RequestMapping(value = {"deleteForAjax"})
	@ResponseBody
	public Map<String, Object> deleteForAjax(AutoDbQuery autoDbQuery) {
		Map<String, Object> map = new HashMap<String, Object>();
		int ret=autoDbService.deleteById(autoDbQuery.getId());
		if(ret==0)
			map.put("result", false);
		else
			map.put("result", true);
		return map;
	}

}

